package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.util.HttpHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/3/1.
 * 维修完成推送短信
 */
public class RepairCompleteSendSmsThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private String hospitalName;        //医院名称

    private String serverName;          //服务商名称

    private String deviceName;          //设备名称

    private String engineerName;        //工程师名称

    private String engineerMobile;      //工程师电话

    private String createPersonMobile;  //申报人电话

    private List<String> systemMobile;  //平台电话

    private List<String> serverMobile;  //服务商电话

    private String sendSmsHospitalForRepairCompleteUrl; //维修完成医院收到短信URL

    private String sendSmsSSForRepairCompleteUrl;   //维修完成平台和厂家收到短信URL

    public RepairCompleteSendSmsThread(String hospitalName, String serverName, String deviceName, String engineerName,
                                       String engineerMobile, String createPersonMobile, List<String> systemMobile,
                                       List<String> serverMobile, String sendSmsHospitalForRepairCompleteUrl,
                                       String sendSmsSSForRepairCompleteUrl) {
        this.hospitalName = hospitalName;
        this.serverName = serverName;
        this.deviceName = deviceName;
        this.engineerName = engineerName;
        this.engineerMobile = engineerMobile;
        this.createPersonMobile = createPersonMobile;
        this.systemMobile = systemMobile;
        this.serverMobile = serverMobile;
        this.sendSmsHospitalForRepairCompleteUrl = sendSmsHospitalForRepairCompleteUrl;
        this.sendSmsSSForRepairCompleteUrl = sendSmsSSForRepairCompleteUrl;
    }

    @Override
    public void run() {
        try {
            Map<String,String> createPersonMap = new HashMap<String,String>();
            createPersonMap.put("mobile",createPersonMobile);
            String createPersonMessag = serverName+"%&"+deviceName+"%&"+engineerName+"%&"+engineerMobile;
            createPersonMap.put("message",createPersonMessag);
            String jsonResult = HttpHelper.postRequestResponseBodyAsString(sendSmsHospitalForRepairCompleteUrl,createPersonMap);
            logger.info("维修完成推送给申报人的短信设备名称："+deviceName+",短信发送结果:"+jsonResult);
        } catch (Exception e) {
            logger.info("维修完成推送给申报人的短信设备名称："+deviceName+",短信发送失败:"+e);
        }

        try {
            StringBuilder sb = new StringBuilder();
            for(String phone : serverMobile){
                if(StringUtil.isEmpty(phone))
                    continue;
                sb.append(phone).append(",");
            }
            for(String phone : systemMobile){
                if(StringUtil.isEmpty(phone))
                    continue;
                sb.append(phone).append(",");
            }
            String mobile = sb.substring(0, sb.length() - 1);
            String message = hospitalName+"%&"+deviceName+"%&"+engineerName+"%&"+engineerMobile;
            Map<String,String> serverMap = new HashMap<String,String>();
            serverMap.put("mobile",mobile);
            serverMap.put("message",message);
            String jsonResult = HttpHelper.postRequestResponseBodyAsString(sendSmsSSForRepairCompleteUrl,serverMap);
            logger.info("维修完成推送平台和服务商的短信设备名称："+deviceName+",短信发送结果:"+jsonResult);
        } catch (Exception e) {
            logger.info("维修完成推送平台和服务商的短信设备名称："+deviceName+",短信发送失败:"+e);
        }
    }
}
